package com.my.screenadapter.aidlDemo;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.R;
import com.my.screenadapter.myHandler.WeakHandler;
import com.testmodelconnectionapp.HandlerService;

import butterknife.BindView;

/**
 * 跨进程通信
 */
public class IPCMessengerActivity extends BaseActivity {

    private static final String TAG = "IPCMessengerActivity";
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.btn_startservice)
    Button btnStartservice;
    Messenger mServiceMessenger;
    private Handler mHandler = new mWeakHandler(this) {

        @Override
        public void baseHandleMessage(Message msg, IPCMessengerActivity obj) {
            switch (msg.what) {

                case HandlerService.Handler_SendToAidlActivityMsg:
                    String obj1 = (String) msg.obj;
                    Toast.makeText(IPCMessengerActivity.this, "接收到 来自service的消息: " + obj1, Toast.LENGTH_SHORT).show();
                    break;

                default:
                    Log.i(TAG, "baseHandleMessage: ===" + msg.what);
                    break;
            }
        }

    };

    @Override
    protected void init() {


        bindService(new Intent(IPCMessengerActivity.this, HandlerService.class), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                Log.i(TAG, "onServiceConnected: ");
                mServiceMessenger = new Messenger(service);

                Messenger ActivityMessenger = new Messenger(mHandler);
                Message obtain = Message.obtain();
                obtain.what = HandlerService.Handler_ActivityMSG;
                obtain.replyTo = ActivityMessenger;

                try {
                    mServiceMessenger.send(obtain);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, Service.BIND_AUTO_CREATE);

        btnStartservice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message obtain = Message.obtain();
                obtain.what=HandlerService.Handler_Start;
                obtain.obj="activity action start";
                try {
                    if (mServiceMessenger != null) {
                        mServiceMessenger.send(obtain);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    ;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_aidl_demo;
    }

    abstract static class mWeakHandler extends WeakHandler<IPCMessengerActivity> {
        public mWeakHandler(IPCMessengerActivity reference) {
            super(reference);
        }
    }

}
